package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要删除的图书.....");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //寻找要删除的书
        int index = -1;
        for (int i = 0; i < bookList.getUsedsize(); i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }

        if(index == -1){
            System.out.println("没有你要删除的书！！！");
            return;
        }

        //确认图书信息
        System.out.println("请确认要删除的图书信息：");
        Book fbook = bookList.getBook(index);
        System.out.println(fbook.toString());
        System.out.println("确认删除输入Yes/不确认输入No");
        String n = scanner.nextLine();

        if(n.equals("Yes")){
            //使用ArrayList的remove方法删除
            bookList.getBooks().remove(index);
            System.out.println("删除成功！");
        }else{
            System.out.println("取消删除");
        }
    }
}
